<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="商户"
            filterable
          >
            <el-option
              v-for="item in payMerchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
            <el-input clearable v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
          <el-form-item>
              <el-input clearable placeholder="商户名称" v-model="form.merchant_name"></el-input>
          </el-form-item> -->
        <el-form-item>
          <el-input clearable v-model="form.order_no" placeholder="委托单号" />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.buy_currency"
            placeholder="买入币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.key"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.sell_currency"
            placeholder="卖出币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.key"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.status"
            placeholder="挂单状态"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in statusList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="order_no"
            label="委托单号"
            align="center"
            width="260"
          />
          <el-table-column
            prop="merchant_id"
            label="商户ID"
            align="center"
            width="110"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="150"
          />
          <el-table-column
            prop="buy_currency"
            label="买入币种"
            align="center"
          />
          <el-table-column
            prop="sell_currency"
            label="卖出币种"
            align="center"
          />
          <el-table-column
            prop="sell_amount"
            label="卖出总金额"
            align="center"
          />
          <el-table-column
            prop="deal_amount"
            label="已卖出金额"
            align="center"
          />
          <el-table-column prop="min_amount" label="最小金额" align="center" />
          <el-table-column prop="max_amount" label="最大金额" align="center" />
          <el-table-column label="价格类型" align="center">
            <template #default="scope">
              {{ scope.row.price_type == "fixed" ? "固定价" : "浮动价" }}
            </template>
          </el-table-column>
          <el-table-column prop="fixed_price" label="固定价格" align="center">
            <template #default="scope">
              {{
                scope.row.price_type == "fixed" ? scope.row.fixed_price : "--"
              }}
            </template>
          </el-table-column>
          <el-table-column label="浮动比例" align="center">
            <template #default="scope">
              {{
                scope.row.price_type == "fixed"
                  ? "--"
                  : scope.row.price_ratio + "%"
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="挂单状态"
            align="center"
            :formatter="formatterStatus"
          />
          <el-table-column
            prop="createtime"
            label="创建时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatterTime, timestamp, pickerDefaultTime } from "@/utils/utils";
import { exchangeEntrustApi } from "@/api/payServerse/exchange";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const payMerchantList = computed(() => globalStore.payMerchantList);

const loading = ref(false);
const tableData = ref([]);
const currencyList = ref([]); //币种
const formData = {
  merchant_id: "",
  merchant_name: "",
  order_no: "",
  buy_currency: "",
  sell_currency: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const statusList = [
  { type: "0", name: "正常" },
  { type: "1", name: "暂停" },
  { type: "2", name: "已取消" },
];
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    buy_currency: form.buy_currency,
    sell_currency: form.sell_currency,
    order_no: form.order_no,
    status: form.status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

//格式化
const formatterStatus = (row, column, cellValue, index) => {
  let name = "";
  statusList.filter((item) => {
    if (item.type == cellValue) {
      name = item.name;
    }
  });
  return name;
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await exchangeEntrustApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
// 获取币种
//const getCurrencyData = async () => {
//    const res = await GetCurrencyApi();
//    if (res.length) {
//        currencyList.value = res.map((item) => {
//            return { value: item, label: item };
//        });
//    }
//};
</script>
